package com.pic.mgt.syushin.executer.bucket.backup;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import com.pic.mgt.file.io.FileOperation;
import com.pic.mgt.log.IgxLog;
import com.pic.mgt.utils.DateConvertUtils;

public class BucketPhysicalBackup implements BucketBackup {
	
	private String bizConfirmationFilenameSuffixStr;
	private String syushinBucketBackupPathStr;
	private FileOperation fileOperation;

	@Override
	public boolean backupBucket(File bucketFile) {
		// 先取得 bucket file 的確認檔
		File confirmationFileForBucketFile = new File(bucketFile.getAbsolutePath() + 
													  this.bizConfirmationFilenameSuffixStr);
		
		// 檢查 bucket file 存在
		if (!bucketFile.exists()) {
			// ********************** Log **********************
			IgxLog.messageLog.error(this,
							"SUDA-292 Backup bucket '{}' process failed. Due to bucket file '{}' doesn't exist.",
							bucketFile.getName(), bucketFile.getAbsolutePath());
			// ********************** Log **********************
			
			return false;
		}
		
		// 檢查 bucket file 的確認檔存在
		if (!confirmationFileForBucketFile.exists()) {
			// ********************** Log **********************
			IgxLog.messageLog.error(this,
							"SUDA-293 Backup confirmation file '{}' for bucket '{}' process failed. Due to confirmation file '{}' doesn't exist.",
							confirmationFileForBucketFile.getName(), bucketFile.getName(), confirmationFileForBucketFile.getAbsolutePath());
			// ********************** Log **********************
			
			return false;
		}
		
		// -------------------------   以上為檢查動作  ------------------------------
		
		
		// 取得 bucket 備份路徑
		File bucketBackupPath = getBackupBucketPath();
		
		// 開始備份實體檔案
		
		// 備份 bucket file
		try {
			// ********************** Log **********************
			if (IgxLog.messageLog.isDebugMode()) {
				IgxLog.messageLog.debug(this,
						"Backuping bucket file '{}'.", bucketFile.getName());
			}
			// ********************** Log **********************
			
			// throws IOException
			this.fileOperation.copyFileToFolder(bucketFile, bucketBackupPath);
			
			// ********************** Log **********************
			if (IgxLog.messageLog.isDebugMode()) {
				IgxLog.messageLog.debug(this,
						"Backuping bucket file '{}' completed.", bucketFile.getName());
			}
			// ********************** Log **********************
		} catch (IOException e) {
			// ********************** Log **********************
			IgxLog.messageDetailsLog.error(this, e.getMessage());
			// ********************** Log **********************
			
			// ********************** Log **********************
			IgxLog.messageLog.error(this,
					"SUDA-294 Backuping bucket file '{}' occured an I/O error.",
					bucketFile.getAbsolutePath());
			// ********************** Log **********************
			
			return false;
		}
		
		// 備份 bucket file 的確認檔
		try {
			// ********************** Log **********************
			if (IgxLog.messageLog.isDebugMode()) {
				IgxLog.messageLog.debug(this,
						"Backuping confirmation file '{}' for bucket '{}'.",
						confirmationFileForBucketFile.getName(), bucketFile.getName());
			}
			// ********************** Log **********************
			
			// throws IOException
			this.fileOperation.copyFileToFolder(confirmationFileForBucketFile, bucketBackupPath);
			
			// ********************** Log **********************
			if (IgxLog.messageLog.isDebugMode()) {
				IgxLog.messageLog.debug(this,
						"Backuping confirmation file '{}' for bucket '{}' completed.",
						confirmationFileForBucketFile.getName(), bucketFile.getName());
			}
			// ********************** Log **********************
		} catch (IOException e) {
			// ********************** Log **********************
			IgxLog.messageDetailsLog.error(this, e.getMessage());
			// ********************** Log **********************
			
			// ********************** Log **********************
			IgxLog.messageLog.error(this,
					"SUDA-295 Backuping confirmation file '{}' for bucket '{}' occured an I/O error.",
					confirmationFileForBucketFile.getAbsolutePath(), bucketFile.getName());
			// ********************** Log **********************
			
			return false;
		}
		
		return true;
	}
	
	@Override
	public File getBackupBucketPath() {
		return getPathWithNowDate(this.syushinBucketBackupPathStr);
	}
	
	@Override
	public File getBackupBucketPath(Date specificDate) {
		return getPathWithNowDate(this.syushinBucketBackupPathStr, specificDate);
	}
	
	/**
	 *  將路徑字串內的關鍵字部份填入現在日期（年，月，日）
	 * @param path
	 * @return
	 */
	private File getPathWithNowDate(String path) {
		String pathWithNowDate = DateConvertUtils.fillDateFieldsInString(path);
		return new File(pathWithNowDate);
	}
	
	/**
	 * 將路徑字串內的關鍵字部份填入指定的日期（年，月，日）
	 * @param path
	 * @param specificDate
	 * @return
	 */
	private File getPathWithNowDate(String path, Date specificDate) {
		Calendar assignCal = Calendar.getInstance();
		assignCal.setTime(specificDate);
		
		String pathWithSpecificDate = DateConvertUtils.fillDateFieldsInString(path, assignCal);
		return new File(pathWithSpecificDate);
	}
	
	// --- getter and setter ---

	public void setSyushinBucketBackupPathStr(String syushinBucketBackupPathStr) {
		this.syushinBucketBackupPathStr = syushinBucketBackupPathStr;
	}

	public void setFileOperation(FileOperation fileOperation) {
		this.fileOperation = fileOperation;
	}

	public void setBizConfirmationFilenameSuffixStr(
			String bizConfirmationFilenameSuffixStr) {
		this.bizConfirmationFilenameSuffixStr = bizConfirmationFilenameSuffixStr;
	}
}
